import re
from typing import List

import jieba.posseg as pseg
from snownlp import SnowNLP

from sam.util.fileUtil2 import write_file_quick
from sam.util.strUtil import filter_symbol_4_word

default_valid_ist = ['n', 'nr', 'ns', 'nt', 'nz', 'ag', 'a', 'ad', 'an', 'v', 'vn']


def cut_stats(line_list: List[str], filter_re=None, valid_ist=None) -> List[List[str]]:
    if valid_ist is None:
        valid_ist = default_valid_ist
    all_list = [['分词', '词性', '数量']]
    word_count = {}
    word_type = {}
    for line in line_list:
        if line:
            new_line = filter_symbol_4_word(line)
            if filter_re:
                new_line = re.sub(filter_re, "", new_line)
            words = pseg.cut(new_line)
            for word, flag in words:
                if flag in valid_ist:
                    c = word_count.get(word, 0) + 1
                    word_count[word] = c
                    word_type[word] = flag

    new_word_count = dict(sorted(word_count.items(), key=lambda d: d[1], reverse=True))
    for word, count in new_word_count.items():
        _type = word_type.get(word)
        _line = [word, _type, count]
        all_list.append(_line)
    return all_list


def cut_stats_export(line_list: List[str], export_file_name, filter_re=None, valid_ist=None):
    all_list = cut_stats(line_list, filter_re=filter_re, valid_ist=valid_ist)
    write_file_quick(
        data_list=all_list
        , export_file_name=export_file_name
        , optional="cover"
        , file_type="xlsx"
    )


def single_stats(line_list: List[str], filter_re=None) -> dict:
    word_count = {}
    for line in line_list:
        if line:
            new_line = filter_symbol_4_word(line)
            if filter_re:
                new_line = re.sub(filter_re, "", new_line)
            for word in new_line:
                c = word_count.get(word, 0) + 1
                word_count[word] = c

    new_word_count = dict(sorted(word_count.items(), key=lambda d: d[1], reverse=True))
    return new_word_count


def sentiments(content: str) -> float:
    """
    得分在 [0, 1] 区间内，越接近 1 则情感越积极，反之则越消极。一般来说，得分大于 0.5 的归于正向情感，小于的归于负向。
    """
    s = SnowNLP(content)
    return s.sentiments


if __name__ == "__main__":
    # stats = single_stats(["张三", "三哥"])
    # print(stats)
    f = sentiments("这个歌不好听,非常难听")
    print(f)
